package org.coody.framework.elock.test;

import java.util.ArrayList;
import java.util.List;

import org.coody.framework.elock.LockHandle;
import org.coody.framework.elock.redis.ELockCache;

public class ELockTester {

	/**
	 * 要加锁的key
	 */

	static {
		/**
		 * 初始化jedis连接
		 */
		ELockCache.initJedisPool("127.0.0.1", 6379, null, 10000);
	}

	public static void main(String[] args) {
		List<Thread> threads = new ArrayList<Thread>();
		for (int j = 0; j < 10; j++) {
			String key = "TESTLOCK_00" + (j + 1);
			for (int i = 0; i < 10; i++) {
				Thread thread = new Thread(new Runnable() {
					@Override
					public void run() {
						test(key);
					}
				});
				threads.add(thread);
			}
		}
		// 启动十个线程
		for (Thread thread : threads) {
			thread.start();
		}
	}

	// 要锁的方法
	private static void test(String key) {
		try {
			LockHandle.lock(key, 100);
			for (int i = 0; i < 10; i++) {
				System.out.println(key + ">>" + i);
				Thread.sleep(100l);
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			LockHandle.unLock(key);
		}
	}
}
